package org.study.meilisearch.boot.controller;

import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.model.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 作用描述
 *
 * @author doveylovey
 * @version v1.0.0
 * @email 1135782208@qq.com
 * @date 2025年04月27日
 */
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@RestController
@RequestMapping("/meilisearch")
public class MeilisearchController {
    private final Client client;

    /**
     * 获取meilisearch服务的健康状态
     */
    @GetMapping("/health")
    public Object health() {
        String health = client.health();
        Boolean isHealthy = client.isHealthy();
        Stats stats = client.getStats();
        String version = client.getVersion();
        Results<Key> keys = client.getKeys();
        return stats;
    }

    /**
     * 获取所有索引信息
     */
    @GetMapping("/getIndexes")
    public Object getIndexes() {
        // 直接返回 Results<Index> 会报错，不知道啥原因
        Results<Index> indexResults = client.getIndexes();
        String rawIndexes = client.getRawIndexes();
        return rawIndexes;
    }

    /**
     * 获取指定索引信息
     */
    @GetMapping("/getIndex/{indexName}")
    public Index getIndex(@PathVariable("indexName") String indexName) {
        Index index = client.index(indexName);
        Index getIndex = client.getIndex(indexName);
        return index;
    }

    /**
     * 获取指定索引设置
     */
    @GetMapping("/getSettings/{indexName}")
    public Settings getSettings(@PathVariable("indexName") String indexName) {
        Settings settings = client.index(indexName).getSettings();
        return settings;
    }

    /**
     * 刪除指定索引
     */
    @DeleteMapping("/{indexName}")
    public TaskInfo deleteIndex(@PathVariable("indexName") String indexName) {
        TaskInfo info = client.deleteIndex(indexName);
        return info;
    }
}
